import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

const routes = [
    // {
    //     path: '/',
    //     redirect: '/login'
    // },
    // {
    //     path: '/about',
    //     name: 'about',
    //     // route level code-splitting
    //     // this generates a separate chunk (about.[hash].js) for this route
    //     // which is lazy-loaded when the route is visited.
    //     component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    // },
    {
        path: '/login',
        name: 'login',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/login.vue')
    },
    {
        path: '/registered',
        name: 'registered',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/registered.vue')
    },
    {
        path: '/',
        name: 'lyout',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../components/lyout/index.vue'),
        children: [
            {
                path: '',
                redirect: 'user'
            },
            {
                path: 'user',
                name: 'user',
                component: () => import('../views/userInfo')
            },
            {
                path: 'video',
                name: 'video',
                component: () => import('../views/video')
            },
            {
                path: 'datas',
                name: 'datas',
                component: () => import('../views/datas')
            }
        ]
    }
];

const originalPush = VueRouter.prototype.push;
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};
const router = new VueRouter({
    routes
});

router.beforeEach((to, from, next) => {
    // if (localStorage.getItem('name')) {
    //     next();
    // } else {
    //     if (to.path === '/login') {
    //         next();
    //     } else {
    //         next('/login');
    //     }
    // }
    // user=>没有name=>登录=>路由拦截=>没有name=>登录
    if (localStorage.getItem('name')) {
        next();
    } else {
        // 判断去的页面是不是登录页 如果是就正常让它跳 如果不是就跳登录页
        // console.log(111);
        // next('/login');
        if (to.path === '/login' || to.path === '/registered') {
            // 判断去的地方是不是登录页
            // 如果是我就正常放行
            next();
        } else {
            // 如果不是
            // 我就把你跳去登录页
            next('/login');
        }
    }
});
export default router;
